//
//  SurnameManager.m
//  projectOfTeamSample
//
//  Created by lk on 15/11/16.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "SurnameManager.h"
#import "URLHeader.h"


@interface SurnameManager ()

// 容纳text的数组
@property (nonatomic, strong) NSMutableArray *SurnameTextArray;

// 容纳pic数组
@property (nonatomic, strong) NSMutableArray *SurnamePicArray;

// 容纳video数组
@property (nonatomic, strong) NSMutableArray *SurnameVideoArray;

// AVPlayer
@property (nonatomic, strong) AVPlayer *player;

@end

@implementation SurnameManager

// 初始化单例
static SurnameManager *manager = nil;
+ (SurnameManager *)managerDefault
{
    @synchronized(self)
    {
        if (manager == nil)
        {
            manager = [[SurnameManager alloc] init];
        }
    }
    
    return manager;
}

#pragma mark - 获取和解析

// 解析Surname中的段子的数据
- (void)getSurnameOfTextWithHandel:(void (^)())handel
{
    // session解析
    NSURLSessionDataTask *sessionDataTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:textURL] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        // 判断
        if (error || !data)
        {
            NSLog(@"解析错误");
            
            return ;
        }
        
        // 清除数组之前的数据
        // [self.SurnameTextArray removeAllObjects];
        
        // 获取jsonDict
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        // 逐层解析 获取data字典
        NSDictionary *dataDict = jsonDict[@"data"];
        
        // 获取data数组
        NSArray *dataArr = dataDict[@"data"];
        
        // 遍历data数组
        for (NSDictionary *groupDict in dataArr)
        {
            SurnameInfo *surnameInfo = [[SurnameInfo alloc] init];
            [surnameInfo setValuesForKeysWithDictionary:groupDict[@"group"]];
            [self.SurnameTextArray containsObject:surnameInfo];
            [self.SurnameTextArray addObject:surnameInfo];
        }
        
        // 在主线程中回调handel，更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            handel();

        });
        
    }];
    
    // 请求数据
    [sessionDataTask resume];
    
}

// 获取surnameInfo
- (SurnameInfo *)getSurnameInfoWithIndex:(NSInteger)index
{
    SurnameInfo *surnameInfo = [[SurnameInfo alloc] init];
    
    surnameInfo = self.SurnameTextArray[index];
    
    return surnameInfo;
}



// 解析Surname中的笑图的数据
- (void)getSurnameOfPicWithHandel:(void (^)())handel
{
    // sesion解析
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:picURL] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        if (error || !data)
        {
            NSLog(@"解析数据出错");
            
            return ;
        }
        
        // 清除数组之前的数据
        // [self.SurnamePicArray removeAllObjects];
        
        // 获取josnDict
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        // 获取data数组
        NSDictionary *dataDict = jsonDict[@"data"];
        
        // 获取data字典
        NSArray *dataArr = dataDict[@"data"];
        
        for (NSDictionary *groupDict in dataArr)
        {
            SurnameOfPicInfo *surnameOfPicInfo = [[SurnameOfPicInfo alloc] init];
            [surnameOfPicInfo setValuesForKeysWithDictionary:groupDict[@"group"]];
            [self.SurnamePicArray containsObject:surnameOfPicInfo];
            [self.SurnamePicArray addObject:surnameOfPicInfo];
        }
        
        
        // 在主线程中回调
        dispatch_async(dispatch_get_main_queue(), ^{
            
            handel();
        });
        
    }];
    
    // 请求解析
    [dataTask resume];
}

// 获取SurnameOfPicInfo
- (SurnameOfPicInfo *)getSurnameOfPicInfoWithIndex:(NSInteger)index
{
    SurnameOfPicInfo *surnameOfPicInfo = [[SurnameOfPicInfo alloc] init];
    
    surnameOfPicInfo = self.SurnamePicArray[index];
    
    return surnameOfPicInfo;
}


// 解析Surname中的视频数据
- (void)getSurnameOfVideoWithHandel:(void (^)())handel
{
    // session解析
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:videoURL] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        // 判断解析是否有错误
        if (error || !data)
        {
            NSLog(@"解析错误");
            
            return ;
        }
        
        // 获取最外层的josnDict
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        // 获取dataDict
        NSDictionary *dataDict = jsonDict[@"data"];
        
        // 获取dataArr
        NSArray *dataArr = dataDict[@"data"];
        
        for (NSDictionary *groupDict in dataArr)
        {
            SurnameOfVideoInfo *surnameOfVideoInfo = [[SurnameOfVideoInfo alloc] init];
            [surnameOfVideoInfo setValuesForKeysWithDictionary:groupDict[@"group"]];
            [self.SurnameVideoArray containsObject:surnameOfVideoInfo];
            [self.SurnameVideoArray addObject:surnameOfVideoInfo];
        }
        
        // 在主线程中回调handel
        dispatch_async(dispatch_get_main_queue(), ^{
            
            handel();
            
        });
        
    }];
    
    // 请求数据
    [dataTask resume];
    
}


// 获取SurnameOfVideoInfo
- (SurnameOfVideoInfo *)getSurnameOfVideoInfoWithIndex:(NSInteger)index
{
    SurnameOfVideoInfo *surnameOfVideo = [[SurnameOfVideoInfo alloc] init];
    surnameOfVideo = self.SurnameVideoArray[index];
    
    return surnameOfVideo;
}

#pragma mark - AVPlayer

// 准备播放音乐
- (AVPlayer *)prepareWithIndex:(NSUInteger)index
{
    SurnameOfVideoInfo *surnameOfVideoInfo = [self getSurnameOfVideoInfoWithIndex:index];
    
    AVPlayerItem *playerItem =[[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:surnameOfVideoInfo.playerurl]];
    
    [self.player replaceCurrentItemWithPlayerItem:playerItem];
    
    return self.player;
}




#pragma mark - Lazy

// SurnameTextArray数组
- (NSMutableArray *)SurnameTextArray
{
    if (_SurnameTextArray == nil)
    {
        _SurnameTextArray = [[NSMutableArray alloc] init];
    }
    
    return _SurnameTextArray;
}


// SurnameTextArray数组个数
- (NSUInteger)surnameCount
{
    return self.SurnameTextArray.count;
}

// SurnamePicArray数组
- (NSMutableArray *)SurnamePicArray
{
    if (_SurnamePicArray == nil)
    {
        _SurnamePicArray = [[NSMutableArray alloc] init];
    }
    
    return _SurnamePicArray;
}

// SurnamePicArray数组个数
- (NSUInteger)surnameOfPicCount
{
    return self.SurnamePicArray.count;
}

// SurnameVideoArray数组
- (NSMutableArray *)SurnameVideoArray
{
    if (_SurnameVideoArray == nil)
    {
        _SurnameVideoArray = [[NSMutableArray alloc] init];
    }
    
    return _SurnameVideoArray;
}

// SurnameVideoArray数组个数
- (NSUInteger)surnameOfVideoCount
{
    return self.SurnameVideoArray.count;
}

// AVPlayer懒加载
- (AVPlayer *)player
{
    if (_player == nil)
    {
        _player = [[AVPlayer alloc] init];
    }
    
    return _player;
}




@end
